/**
 * System management including user, resource, role
 */
var user = {
	/**
	 * Show user list
	 */
	list : function() {
		// Request user list url from UserController.
		var userListUrl = baseURL + "/system/user/list"
		$.ajax({
			type : "get",
			url : userListUrl,
			dataType : "html",
			success : function(html) {
				// Request successfully and fill the data.
				$("#userList").empty().html(html);
			}
		});
	},
	/**
	 * New/Edit user
	 */
	edit : function() {
		// Get the first userId from checked buttons.
		var select = new Array();;
		$("input[name='userId']:checked").each(function(){
			select.push($(this).val()); 
            }); 
		// The title of edit dialog
		var title = localeMsg.user.edit;
		// If userId is not empty, edit the user info; or new it.
		if (select.length == 0) {
			common.showTip({
				title : localeMsg.common.warn,
				text : localeMsg.common.selectMsg
			});
		}
		else if(select.length>1){
			common.showTip({
				title : localeMsg.common.warn,
				text : localeMsg.common.selectMsg
			});
		}
		else{
			var param = {};
			var userId = $("input[name='userId']:checked").eq(0).val();
			param.userId = userId;
			common.dialog({
				obj : "#editUserDialog",
				url : baseURL + "/system/user/edit",
				param : param,
				title : title,
				width : 400,
				confirm : function() {
					if($("#editUserForm").form('validate')==true){
						// Click the "Confirm" button to save user info.
						user.save();
					}
					else{
						common.showTip({
							title : localeMsg.common.warn,
							text : localeMsg.common.invalidData
						});
					}
				}
			});
		}

	},
	

	create : function() {

		// The title of edit dialog
		var title = localeMsg.user.create;
		var param = {};
		common.dialog({
			obj : "#editUserDialog",
			url : baseURL + "/system/user/edit",
			param : param,
			title : title,
			width : 400,
			confirm : function() {
				if($("#editUserForm").form('validate')==true){
					// Click the "Confirm" button to save user info.
					user.save();
				}
				else{
					common.showTip({
						title : localeMsg.common.warn,
						text : localeMsg.common.invalidData
					});
				}
			}
		});
	},
	
	/**
	 * user name 
	 */
	userName : function(value) {
		var Url = baseURL + "/system/user/userName"
		$.ajax({
			type : "get",
			url : Url,
			data : {userName : value},
			dataType : "json",
			success : function(json) {
				// Request successfully and fill the data.
				$("#userList").empty().html(html);
			}
		});
	},
	
	/**
	 * Save pool
	 */
	save : function() {
		if( $("#editPassword").eq(0).val()==$("#confirmPassword").eq(0).val()){
			$("#editUserForm").ajaxSubmit({
				success : function(json) {
					// Save successfully and refresh the user list.
					if (json.result && json.result == "success") {
						common.showTip({
							success : true,
							title : localeMsg.common.info,
							text : localeMsg.common.saveSuccess,
							callback : function() {
								user.list();
							},
						});
					} else if(json.result && json.result == "noauthority"){
						common.showTip({
							title : localeMsg.common.warn,
							text : "您没有创建网盘用户的权限!",
							success : false
						});
						/*var confirmMessage = "您没有创建网盘用户的权限，是否仅创建系统用户?";
						common.confirmDialog(confirmMessage,user.createSysUserOnly);*/
					}else{
						common.showTip({
							title : localeMsg.common.warn,
							text : localeMsg.common.saveFailed,
							success : false
						});
					}
				}
			});
		}
		else{
			common.showTip({
				title : localeMsg.common.warn,
				text : localeMsg.user.password,
			});
		}
	},
	
	createSysUserOnly : function() {
		//if( $("#editPassword").eq(0).val()==$("#confirmPassword").eq(0).val()){
			$("#editUserForm").ajaxSubmit({
				success : function(json) {
					// Save successfully and refresh the user list.
					if (json.result && json.result == "noauthority") {
						common.showTip({
							success : true,
							title : localeMsg.common.info,
							text : localeMsg.common.saveSuccess,
							callback : function() {
								user.list();
							},
						});
					} else {
						common.showTip({
							title : localeMsg.common.warn,
							text : localeMsg.common.saveFailed,
							success : false
						});
					}
				}
			});
		//}
		/*else{
			common.showTip({
				title : localeMsg.common.warn,
				text : localeMsg.user.password,
			});
		}*/
	},
	/**
	 * Remove users
	 */
	remove : function(){
		var userId=[];
		$('input[name="userId"]:checked').each(function(){
			userId.push($(this).val()); 
            });   
		if (Object.keys(userId).length === 0){
			common.showTip({
				title : localeMsg.common.warn,
				text : localeMsg.common.selectMsg
			});
		}else{
			var confirmMessage = localeMsg.common.relateddeleteconfirm;
				common.confirmDialog(confirmMessage,user.removeDel);
		}
	},
	
	
	removeDel : function() {
		//  Remove  records
		var userId=[];
		$('input[name="userId"]:checked').each(function(){
			userId.push($(this).val()); 
            });  
		
		$.ajax({
			type : "post",
			url : baseURL + "/system/user/remove",
			data : {userId : userId},
			traditional: true,
			dataType : "json",
			success : function(json) {
				if (json.result && json.result == "success") {
					common.showTip({
						success : true,
						title : localeMsg.common.info,
						text : localeMsg.common.deleteSuccess,
						callback : function() {
							user.list();
						}
					});
				}else if(json.result && json.result == "wrong"){
					common.showTip({
						title : localeMsg.common.warn,
						text : "已为该用户分配桶，请先将其移除或删除再进行该操作",
						success : false
					});
					
				} else if(json.result && json.result == "admin"){
					common.showTip({
						title : localeMsg.common.warn,
						text : localeMsg.user.admin_remove,
						success : false
					});
					
				}else {
					common.showTip({
						success : false,
						title : localeMsg.common.warn,
						text : localeMsg.common.deleteFailed,
					});
				}
			}
		});
	},
	
	status : function(){
		$('#userlist td[name="status"]').each(function(){
			if($(this).attr("value") == 0){
				$(this).text(localeMsg.common.inactive);
			}
			else{
				 $(this).text(localeMsg.common.active);
			}
	     });
	},
	
	resetPassword : function(){
		// Get the first userId from checked buttons.
		var select = new Array();;
		$("input[name='userId']:checked").each(function(){
			select.push($(this).val()); 
            }); 
		// The title of edit dialog
		var title = localeMsg.user.reser_password;
		// If userId is not empty, edit the user info; or new it.
		if (select.length == 0) {
			common.showTip({
				title : localeMsg.common.warn,
				text : localeMsg.common.selectMsg
			});
		}
		else if(select.length>1){
			common.showTip({
				title : localeMsg.common.warn,
				text : localeMsg.common.selectMsg
			});
		}
		else{
			var param = {};
			var userId = $("input[name='userId']:checked").eq(0).val();
			param.userId = userId;
			common.dialog({
				obj : "#resetPasswordForm",
				url : baseURL + "/system/user/resetPassword",
				param : param,
				title : title,
				width : 400,
				confirm : function() {
					if($("#resetPasswordForm").form('validate')==true){
						// Click the "Confirm" button to save user info.
						user.resetPasswordsave();
					}
					else{
						common.showTip({
							title : localeMsg.common.warn,
							text : localeMsg.common.invalidData
						});
					}
				}
			});
		}
	},
	
	resetPasswordsave : function(){
		if( $("#NewPassword").eq(0).val()==$("#confirmNewPassword").eq(0).val()){
			var param = {};
			param.newW = $("#NewPassword").eq(0).val();
			param.userId = $("#userId").eq(0).val();
				$.ajax({
					type : "post",
					url : baseURL + "/system/user/resetPasswordsave",
					data : param,
					traditional: true,
					dataType : "json",
					success : function(json) {
						if (json.result && json.result == "success") {
							common.showTip({
								success : true,
								title : localeMsg.common.info,
								text : localeMsg.common.saveSuccess,
							});
						} else {
							common.showTip({
								success : false,
								title : localeMsg.common.warn,
								text : localeMsg.common.saveFailed,
							});
						}
					}
					
				});
			}
			else{
				common.showTip({
					title : localeMsg.common.warn,
					text : localeMsg.user.password,
				});
			}
	},
	
	validate : function(){
		
	    $('#editFullName').validatebox({
	        required: true,
	        validType: ['minLength[1]','maxLength[20]','name']
	    });
	    
	    $('#editUserName').validatebox({
	        required: true,
	        validType:  ['maxLength[20]','vername','uniquename["/system/user/validateName"]']
	    });
	    
	    $('#editPassword').validatebox({
	        required: true,
	        validType: ['minLength[6]','maxLength[50]']
	    });
	    
	    $('#editMobliePhone').validatebox({
	        required: true,
	        validType: ['minLength[11]','maxLength[11]','digits']
	    });
	    
	    $('#editEMail').validatebox({
	        required: true,
	        validType: 'email'
	    });
	    $('#confirmPassword').validatebox({
	        required: true,
	        validType: ['minLength[6]','maxLength[50]']/*,'same[editPassword]'*/
	    });
	},
	
	validateResetPassword : function(){
	    $('#NewPassword').validatebox({
	        required: true,
	        validType: ['minLength[6]','maxLength[50]']
	    });
	    $('#confirmNewPassword').validatebox({
	        required: true,
	        validType: ['minLength[6]','maxLength[50]']
	    });
	},
};